/**
 * 
 */
package by.bsu.scs.presentation.action;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;
import org.apache.struts.util.LabelValueBean;

import by.bsu.scs.db.dao.DisciplineHome;
import by.bsu.scs.db.dao.DisciplineTaskHome;
import by.bsu.scs.db.dao.MarkPolicyHome;
import by.bsu.scs.db.dao.TaskAttachmentHome;
import by.bsu.scs.db.dao.TaskAttachmentStateHome;
import by.bsu.scs.db.dao.TaskHome;
import by.bsu.scs.model.Discipline;
import by.bsu.scs.model.DisciplineInstructor;
import by.bsu.scs.model.DisciplineTask;
import by.bsu.scs.model.MarkPolicy;
import by.bsu.scs.model.Task;
import by.bsu.scs.model.TaskAttachment;
import by.bsu.scs.model.TaskAttachmentState;
import by.bsu.scs.model.User;
import by.bsu.scs.presentation.form.CourseForm;
import by.bsu.scs.util.manager.SessionManager;

/**
 * @author slavchoo
 * 
 */
public class CourseAction extends DispatchAction {

	private static final String EDIT = "edit";
	private static final String CREATE = "create";
	private static final String LIST = "list";
	private static final String ADD = "add";

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
//		CourseForm courseForm = (CourseForm) form;
//		DisciplineHome disciplineDao = new DisciplineHome();
//		User instructor = SessionManager.getInstance().getUser(request);


		return mapping.findForward(LIST);
	}

	/**
	 * Redirect to course_create.jsp
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return mapping forward path
	 */
	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		return mapping.findForward(CREATE);
	}

//	public ActionForward add(ActionMapping mapping, ActionForm form,
//			HttpServletRequest request, HttpServletResponse response)
//			 {
//
//		CourseForm courseForm = (CourseForm) form;
//		// file upload
//		FileOutputStream outputStream = null;
//		FormFile formFile = null;
//		String path = "";
//		try {
//			formFile = courseForm.getFile();
//			path = getServlet().getServletContext().getRealPath("")
//					+ "/" + formFile.getFileName();
//			outputStream = new FileOutputStream(new File(path));
//			try {
//				outputStream.write(formFile.getFileData());
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		} 
//		catch (FileNotFoundException e) {
//			// TODO: handle exception
//		}
//		finally {
//			if (outputStream != null) {
//				try {
//					outputStream.close();
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
//		}
//		
//		//
//		String courseName = courseForm.getCourseName();
//		User user = SessionManager.getInstance().getUser(request);
//
//		// Discipline disciplineModel = new Discipline(user, courseName);
//		Discipline disciplineModel = new Discipline(courseName);
//		DisciplineHome disciplineDao = new DisciplineHome();
//		disciplineDao.persist(disciplineModel);
//		//File bind with attachment
//		//Поработать над параметрами
//		
//		//создаём таск
//		MarkPolicy markPolicy = new MarkPolicyHome().findById(cForm.getMarkPolicyId());
//		Task taskModel = new Task("Литература", "Литература для учебного курса: "+disciplineModel.getDisciplineName(), new Date(Calendar.getInstance().getTimeInMillis()), 0, markPolicy);
//		TaskHome taskDao = new TaskHome();
//		taskDao.persist(taskModel);
//		
//		//Создаём taskAttachmentState
//		TaskAttachmentState taskAttachmentStateModel = new TaskAttachmentState("LITERATURA");
//		TaskAttachmentStateHome taskAttachmentStateDao = new TaskAttachmentStateHome();
//		taskAttachmentStateDao.persist(taskAttachmentStateModel);
//				
//		//Создаём task atach
//		TaskAttachment taskAttachmentModel = new TaskAttachment(taskModel, path, taskAttachmentStateModel, user);
//		TaskAttachmentHome taskAttachmentDao = new TaskAttachmentHome();
//		taskAttachmentDao.persist(taskAttachmentModel);
//		
//		
//		///Привязываем к дисциплине
//		DisciplineTask disciplineTaskModel = new DisciplineTask(disciplineModel.getDisciplineId(), new DisciplineInstructor(user, disciplineModel), taskModel);
//		DisciplineTaskHome disciplineTaskDao = new DisciplineTaskHome();
//		disciplineTaskDao.persist(disciplineTaskModel);
//		
//		return mapping.findForward(ADD);
//	}

	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession();

		return mapping.findForward(EDIT);
	}

}
